/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.sd.jxta;

import co.edu.sd.jxta.msn.MessageMSPFactory;
import co.edu.sd.jxta.service.ServiceType;
import co.edu.sd.jxta.task.IOTask;
import co.edu.sd.jxta.util.FileUtil;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.MessageElement;
import net.jxta.pipe.InputPipe;
import net.jxta.pipe.OutputPipe;
import net.jxta.pipe.OutputPipeEvent;
import net.jxta.pipe.OutputPipeListener;
import net.jxta.pipe.PipeMsgEvent;
import net.jxta.pipe.PipeMsgListener;
import net.jxta.pipe.PipeService;
import net.jxta.protocol.PipeAdvertisement;

/**
 *
 * @author SD
 */
public class PipeHandler
        implements PipeMsgListener, OutputPipeListener {

    private PipeService pipeService;
    private PipeAdvertisement adv;
    private OutputPipe outputPipe;
    private InputPipe inputPipe;
    private static Logger logger = Logger.getLogger(PipeHandler.class.getName());
    private List parameters;
    private String owner;

    public PipeHandler(PipeService pipeService, PipeAdvertisement adv, List parameters, String owner) {
        this.pipeService = pipeService;
        this.adv = adv;
        this.parameters = parameters;
        this.owner = owner;
        try {
            doRequest();
        } catch (IOException ex) {
            logger.log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
    }

    private void doRequest() throws IOException {
        logger.info("doRequest" + adv.getName());
        inputPipe = pipeService.createInputPipe(adv, this);
        outputPipe = pipeService.createOutputPipe(adv, 10000);
         logger.info("--------------------------sending request" + inputPipe.getPipeID() + " " + outputPipe.getPipeID());
        switch (ServiceType.getTypeByID(adv.getName())) {
            case FILE_UPLOAD:
                logger.info("sending message:" + owner);
                outputPipe.send(MessageMSPFactory.createFileUploadMessage("" + adv.getID(), "/project Plan.png"));
                break;
            case GENERAL_MESSAGE:

                break;
        }

    }

    public void pipeMsgEvent(PipeMsgEvent ev) {
        logger.info("request recieved " + inputPipe.getName() + " " + outputPipe.getName());
        Message msg;
        try {
            // Obtain the message from the event
            msg = ev.getMessage();
            if (msg == null) {
                System.out.println("Received an empty message" + inputPipe);
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        switch (ServiceType.getTypeByID(adv.getName())) {
            case FILE_UPLOAD:
                logger.info("writing file on "+owner);
                //IOTask.getInstance().writeFile("" + parameters.get(0), msg);
                /*try {
                    outputPipe.send(MessageMSPFactory.createOperationSuccessMessage());
                } catch (IOException ex) {
                    Logger.getLogger(PipeHandler.class.getName()).log(Level.SEVERE, null, ex);
                }*/
                break;
        }
        //inputPipe.close();
        //outputPipe.close();

    }

    public void outputPipeEvent(OutputPipeEvent ev) {
    }
}
